//
//  NewBillsViewController.m
//  Dormitory2404
//
//  Created by gleeman.G on 2017/12/20.
//  Copyright © 2017年 Hainan Suyun network technology co. LTD. All rights reserved.
//

#import "NewBillsViewController.h"
#import "order.h"
#import "OrderCell.h"
#import "History.h"

@interface NewBillsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) IBOutlet UITextField *causeTextField;


@property (strong,nonatomic)NSMutableArray *dataArray;

@end

@implementation NewBillsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"又花钱";
    [self tableViewSetting];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (self.dataArray.count > 0) {
        History *history = [[History alloc]init];
        history.date = [NSDate date];
        history.orderList = self.dataArray;
//        history.projectName =
        history.totalPrice =  [self totalPrice];
        
        [[HistoryList shareInstance].historyList insertObject:history atIndex:0];
        [HistoryList saveHistoryList];
    }
}

- (void)tableViewSetting{
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([OrderCell class])];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderCell class])];
    order * order = nil ;
    if (indexPath.row < self.dataArray.count) {
        order = self.dataArray[indexPath.row];
    }
    cell.nameLabel.text = order.name;
    cell.priceLabel.text = order.price;
    cell.contentLabel.text = order.content;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.f;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (IBAction)addButtonClick:(id)sender
{
    [self.view endEditing:YES];
    if (self.nameTextField.text.length <1) {
        [self showAlert:@"报告舍长，没写名称"];
        return;
    }
    if (self.priceTextField.text.length < 1)
    {
        [self showAlert:@"报告舍长，没写价钱"];
        return;
    }
    order  *o = [[order alloc]init];
    o.name = self.nameTextField.text;
    o.price = self.priceTextField.text;
    o.content = self.causeTextField.text;
    [self.dataArray addObject:o];
    
    self.nameTextField.text = @"";
    self.priceTextField.text = @"";
    self.causeTextField.text = @"";
    
    float totalPrice = [self totalPrice];
    
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%.2f元",totalPrice];
    NSIndexSet *set = [[NSIndexSet alloc]initWithIndex:0];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    
}

- (float)totalPrice
{
    float totalPrice = 0;
    for (int i = 0; i < self.dataArray.count; i ++) {
        order * item = self.dataArray[i];
        totalPrice += [item.price floatValue];
    }
    return totalPrice;
}

- (void)showAlert:(NSString *)msg
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
@end
